

#####
## issue ownership test
## EG
#####




## data


io = read_excel("cps_Issue_ownership_variable.xlsx")
io$Country[io$Country == "UK"] = "United Kingdom"
io2 = pivot_longer(io, -c(Country,Party,Family, `Comment (family: other)`),
                          names_to = "cap",values_to = "io") |>
  mutate(cap = as.numeric(as.character(cap)))

dc2 = left_join(dc,io2,by= c("country_name"="Country",
                             "party"="Party",
                             "topic" = "cap"))




## analysis



cns = unique(dc2$cn)
cns = cns[cns != "sw"]

testf = function(x){
  form = party_share ~ party_share_last * io + attention_others
  fit = lm(form, data = dc2 |> filter(cn == x))
  fit
}

out = lapply(cns,testf)


rows = tibble::tribble()
mods = out
names(mods) = cns
modelsummary(mods,estimate = "{estimate}{stars}",statistic = NULL,
             coef_rename = c("(Intercept)" = "Constant","party_share_last"="Stability",
                             "io" = "Issue ownership",
                             "attention_others"="Overlap"),
             vcov = "robust", cluster = ~year_party,
             gof_omit = 'IC|Log|RMSE')


testf2 = function(x){
  form = party_share ~ party_share_last * io + attention_others
  fit = lm(form, data = dc2  |> filter(cn == x))
  margs = summary(margins(fit,at = list(io = c(0,1))))
  margs
}

out2 = lapply(cns,testf2)
names(out2) = cns
out2 = bind_rows(out2)
out2$cns = rep(cns,each = 6)
out2$vars = rep(c("Overlap","Issue ownership","Stability"),each=2)






  
